/* hardware_options.cpp -- HardwareOptions dialog class implentation; hardware options dialog is used to set the selected robot's hardware options
 * hardware_options.cpp is part of Robodrome
 *
 * Copyright (c) 2009 Alex French
 * Robodrome is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Robodrome is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "hardware_options.h"
#include "world.h"

HardwareOptions::HardwareOptions(BaseObjectType *cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refXml) :
	Gtk::Dialog(cobject), m_refXml(refXml)
{
	std::stringstream buf;
	std::stringstream errbuf;
	for (int i = 0; i < 3; i++) { // 3-option settings (bullet type, turret shape)
		buf << "bullet_type" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), bullet_type_button[i]);
		if (!bullet_type_button[i]) throw std::runtime_error(errbuf.str());
		bullet_type_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_bullet_type_button_toggled));
		buf.str("");
		errbuf.str("");
		buf << "turret_type" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), turret_type_button[i]);
		if (!turret_type_button[i]) throw std::runtime_error(errbuf.str());
		turret_type_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_turret_type_button_toggled));
		buf.str("");
		errbuf.str("");
	}
	for (int i = 0; i < 4; i++) { // 4-option settings (energy, damage, and shield max)
		buf << "energy_max" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), energy_max_button[i]);
		if (!energy_max_button[i]) throw std::runtime_error(errbuf.str());
		energy_max_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_energy_max_button_toggled));
		buf.str("");
		errbuf.str("");
		buf << "damage_max" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), damage_max_button[i]);
		if (!damage_max_button[i]) throw std::runtime_error(errbuf.str());
		damage_max_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_damage_max_button_toggled));
		buf.str("");
		errbuf.str("");
		buf << "shield_max" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), shield_max_button[i]);
		if (!shield_max_button[i]) throw std::runtime_error(errbuf.str());
		shield_max_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_shield_max_button_toggled));
		buf.str("");
		errbuf.str("");
	}
	for (int i = 0; i < 5; i++) { // 5-option settings (cpu max)
		buf << "cpu_max" << i;
		errbuf << "Couldn't find " << buf.str() << " in interface.glade";
		refXml->get_widget(buf.str(), cpu_max_button[i]);
		if (!cpu_max_button[i]) throw std::runtime_error(errbuf.str());
		cpu_max_button[i]->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_cpu_max_button_toggled));
		buf.str("");
		errbuf.str("");
	}
	refXml->get_widget("missiles_button", missiles_button);
	if (!missiles_button) throw std::runtime_error("Couldn't find missiles_button in interface.glade");
	missiles_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_missiles_button));
	refXml->get_widget("tacnukes_button", tacnukes_button);
	if (!tacnukes_button) throw std::runtime_error("Couldn't find tacnukes_button in interface.glade");
	tacnukes_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_tacnukes_button));
	refXml->get_widget("hellbores_button", hellbores_button);
	if (!hellbores_button) throw std::runtime_error("Couldn't find hellbores_button in interface.glade");
	hellbores_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_hellbores_button));
	refXml->get_widget("mines_button", mines_button);
	if (!mines_button) throw std::runtime_error("Couldn't find mines_button in interface.glade");
	mines_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_mines_button));
	refXml->get_widget("stunners_button", stunners_button);
	if (!stunners_button) throw std::runtime_error("Couldn't find stunners_button in interface.glade");
	stunners_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_stunners_button));
	refXml->get_widget("probes_button", probes_button);
	if (!probes_button) throw std::runtime_error("Couldn't find probes_button in interface.glade");
	probes_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_probes_button));
	refXml->get_widget("no_negative_energy_button", no_negative_energy_button);
	if (!no_negative_energy_button) throw std::runtime_error("Couldn't find no_negative_energy_button in interface.glade");
	no_negative_energy_button->signal_toggled().connect(sigc::mem_fun(*this, &HardwareOptions::on_no_negative_energy_button));

	refXml->get_widget("points_label", points_label);
	if (!points_label) throw std::runtime_error("Couldn't find points_label in interface.glade");
}

void HardwareOptions::init(void)
{
	bullet_type_button[players[selected_player].get_bullet_type()]->set_active();
	turret_type_button[players[selected_player].get_turret_type()]->set_active();
	energy_max_button[players[selected_player].get_energy_max()]->set_active();
	damage_max_button[players[selected_player].get_damage_max()]->set_active();
	shield_max_button[players[selected_player].get_shield_max()]->set_active();
	cpu_max_button[players[selected_player].get_cpu_max()]->set_active();
	missiles_button->set_active(players[selected_player].get_missiles());
	tacnukes_button->set_active(players[selected_player].get_tacnukes());
	hellbores_button->set_active(players[selected_player].get_hellbores());
	mines_button->set_active(players[selected_player].get_mines());
	stunners_button->set_active(players[selected_player].get_stunners());
	probes_button->set_active(players[selected_player].get_probes());
	no_negative_energy_button->set_active(players[selected_player].get_no_negative_energy());
	this->update();
}

void HardwareOptions::on_bullet_type_button_toggled(void)
{
	for (int i = 0; i < 3; i++) {
		if (bullet_type_button[i]->get_active()) {
			players[selected_player].set_bullet_type(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_turret_type_button_toggled(void)
{
	for (int i = 0; i < 3; i++) {
		if (turret_type_button[i]->get_active()) {
			players[selected_player].set_turret_type(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_energy_max_button_toggled(void)
{
	for (int i = 0; i < 4; i++) {
		if (energy_max_button[i]->get_active()) {
			players[selected_player].set_energy_max(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_damage_max_button_toggled(void)
{
	for (int i = 0; i < 4; i++) {
		if (damage_max_button[i]->get_active()) {
			players[selected_player].set_damage_max(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_shield_max_button_toggled(void)
{
	for (int i = 0; i < 4; i++) {
		if (shield_max_button[i]->get_active()) {
			players[selected_player].set_shield_max(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_cpu_max_button_toggled(void)
{
	for (int i = 0; i < 5; i++) {
		if (cpu_max_button[i]->get_active()) {
			players[selected_player].set_cpu_max(i);
			this->update();
			return;
		}
	}
}

void HardwareOptions::on_missiles_button(void)
{
	players[selected_player].set_missiles(missiles_button->get_active());
	this->update();
}
void HardwareOptions::on_tacnukes_button(void)
{
	players[selected_player].set_tacnukes(tacnukes_button->get_active());
	this->update();
}
void HardwareOptions::on_hellbores_button(void)
{
	players[selected_player].set_hellbores(hellbores_button->get_active());
	this->update();
}
void HardwareOptions::on_mines_button(void)
{
	players[selected_player].set_mines(mines_button->get_active());
	this->update();
}
void HardwareOptions::on_stunners_button(void)
{
	players[selected_player].set_stunners(stunners_button->get_active());
	this->update();
}
void HardwareOptions::on_probes_button(void)
{
	players[selected_player].set_probes(probes_button->get_active());
	this->update();
}
void HardwareOptions::on_no_negative_energy_button(void)
{
	players[selected_player].set_no_negative_energy(no_negative_energy_button->get_active());
	this->update();
}

void HardwareOptions::update(void)
{
	std::stringstream buf;

	buf << "<b>Points: " << players[selected_player].hardware_points() << "</b>";
	points_label->set_label(buf.str());
}
